<template>
  <div class="app-container">
    <el-dialog :title="titles[flag]" :visible.sync="dialogVisible" width="50%" append-to-body>
      <el-form ref="form" :model="form" :disabled="flag==1" v-loading="loading">
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="快递名称" prop="expressId" required>
              <div v-if="flag==1">{{ form.expressName }}</div>
              <el-select v-else v-model="form.expressId" filterable style="width: 100%">
                <el-option
                  v-for="item in expressList"
                  :key="item.id"
                  :label="item.expressName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="目的地" prop="proIdList" required>
              <div v-if="flag==1">
                <span v-for="val in form.dstVoList">{{ val.proName }};</span>
              </div>
              <el-select v-else v-model="form.proIdList" filterable multiple style="width: 100%">
                <el-option
                  v-for="item in provinceList"
                  :key="item.id"
                  :label="item.proName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="首重(千克)" prop="firstWeight" required>
              <div v-if="flag==1">{{ form.firstWeight }}</div>
              <el-input v-else type="number" v-model="form.firstWeight" :min="0" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="首重价格(元)" prop="firstPrice" required>
              <div v-if="flag==1">{{ form.firstPrice }}</div>
              <el-input v-else type="number" v-model="form.firstPrice" :min="0" placeholder="请输入"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="续重(千克)" prop="nextPrice" required>
              <div v-if="flag==1">{{ form.nextPrice }}</div>
              <el-input v-else type="number" v-model="form.nextPrice" :min="0" placeholder="请输入"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="续重价格(元/千克)" prop="nextWeight" required>
              <div v-if="flag==1">{{ form.nextWeight }}</div>
              <el-input v-else type="number" v-model="form.nextWeight" :min="0" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogOk" v-if="flag!=1">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import {addExpressPrice, updateExpressPrice} from "../../../../api/expressMng";

export default {
  props: ['expressList', 'provinceList'],
  data() {
    return {
      flag: 0,
      titles: ['新增', '详情', '修改'],
      loading: false,
      dialogVisible: false,
      form: {}
    }
  },
  methods: {
    show(flag, form) {
      this.flag = flag
      this.dialogVisible = true
      this.resetForm('form')
      this.form = form
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          this.loading = true
          if (!this.flag) {
            addExpressPrice(this.form).then(res => {
              this.dialogVisible = false
              this.$emit('ok')
              this.$message.success('新增成功')
            }).finally(e => {
              this.loading = false
            })
          } else {
            updateExpressPrice(this.form).then(res => {
              this.dialogVisible = false
              this.$emit('ok')
              this.$message.success('修改成功')
            }).finally(e => {
              this.loading = false
            })
          }
        }
      })
    },
  }
}
</script>
